package com.hh.bss.project.service.impl;

import static com.hh.bss.util.ObjectUtils.isNotEmpty;

import java.util.ArrayList;
import java.util.List;

import com.hh.bss.base.BaseBusinessService;
import com.hh.bss.base.EntityDao;
import com.hh.bss.common.ConstantDic;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.cache.SystemUserCache;
import com.hh.bss.common.page.Page;
import com.hh.bss.project.dao.SearchProjectDao;
import com.hh.bss.project.entity.ContractJobCvOffer;
import com.hh.bss.project.service.ProjectService;
import com.hh.bss.project.vo.query.ProjectQuery;
import com.hh.bss.util.DateHelper;
import com.hh.bss.util.StringHelper;


public class ProjectServiceImpl extends BaseBusinessService<ContractJobCvOffer,java.lang.Integer>  implements ProjectService {

	private SearchProjectDao searchProjectDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSearchProjectDaoImpl(SearchProjectDao dao) {
		this.searchProjectDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.searchProjectDao;
	}

	
	public Page findProjectPage(ProjectQuery query) {
		// TODO Auto-generated method stub
		//type=1 职位项目 type=2 CC项目 
		
		StringBuffer sql = new StringBuffer();
		//h.jobManager,i.jobCooper
		List values = new ArrayList();
		sql.append(" select * from ( ");		
		sql.append(" select concat('j',tt.id) id,tt.id projectId,'职位项目' projectType,ifnull(h.jobManager,tt.owner) owner,i.jobCooper partner,tt.jobName name,tt.create_dt createDt,tt.custName custName,tt.industry,tt.function from (select a.id,a.creator owner,a.name jobName,a.create_dt ,a.industry,a.function,c.name custName  from contract_job a,customer b,company c where a.creator=? and a.cust_id=b.id and ifnull(a.status,0)=0 and b.company_id=c.id) tt  ");		
		sql.append(" left join  ( select a.job_id,group_concat((b.account) separator ',') as jobManager from contract_job_partner a,system_user b where a.account=b.account and a.role='").append(ConstantDic.JOB_PARTNER_TYPE_MC).append("' group by a.job_id) h on h.job_id=tt.id");
		sql.append(" left join  ( select a.job_id,group_concat((b.account) separator ',') as jobCooper from contract_job_partner a,system_user b where a.account=b.account and a.role='").append(ConstantDic.JOB_PARTNER_TYPE_PC).append("' group by a.job_id) i on i.job_id=tt.id");
		
		//1职位项目，2：CC项目
		
		if (query.getType()==null||query.getType().intValue()==0){			
		
			//创建时间是当年的或启动时间是 当年的
			sql.append(" union all ");
			sql.append(" select concat('p',a.id) id,a.id projectId,'CC项目' projectType,a.account owner,a.partner,a.name,a.create_dt createDt,'' custName,a.industrys industry,a.functions function from search_project a  where a.account=? and ( year(a.create_dt)>=").append(DateHelper.getNowYear()).append(" or year(a.start_d)>=").append(DateHelper.getNowYear()).append(") ");

			if (query.getOnlineUser()!=null){
				values.add(query.getOnlineUser().getUsername());
					
			}else{
				values.add(null);
				
			}
		}
		sql.append(" ) pc ");
		sql.append(" where 1=1 ");
		
		if (query.getOnlineUser()!=null){
			values.add(query.getOnlineUser().getUsername());
				
		}else{
			values.add(null);
			
		}
		System.out.println(sql.toString());

		sql.append(" order by pc.createDt desc ");
		return getEntityDao().getJdbcDao().createPage(sql.toString(),values.toArray(), new MapObjectRowMapper(), query.getPageSize(), query.getPageNumber());
		
		
		//return null;
	}

}
